package rensoft.portfolio.client.model.titles;

import java.util.Iterator;

import static cs.java.lang.Lang.map;

import cs.java.collections.Map;
import cs.java.lang.HasService;



import rensoft.gwt.client.lang.GwtBase;



import rensoft.portfolio.client.model.titles.titles.Contact;
import rensoft.portfolio.client.model.titles.titles.Services;
import rensoft.portfolio.client.model.titles.titles.Works;



public class TitlesImpl extends GwtBase implements Titles {

  private final Map<TitleState, Title> titles = map();
  private TitleState typeState = TitleState.closed;

  public TitlesImpl(HasService parent) {
    initialize(parent);
    titles.put(TitleState.contact, new Contact(this));
    titles.put(TitleState.services, new Services(this));
    titles.put(TitleState.products, new Works(this));
  }

  public Title getOpenTitle() {
    return getTitle(typeState);
  }

  public TitleState getState() {
    return typeState;
  }

  public Title getTitle(TitleState type) {
    return titles.value(type);
  }

  public Iterator<Title> iterator() {
    return titles.values().iterator();
  }

  public void open(TitleState value) {
    if (typeState == value) return;
    typeState = value;
    fireEvent(ON_CHANGE, getOpenTitle());
  }
}
